/* 
 * Displaying a rotating RISCV logo
 * Bruno Levy, 2020
 */ 

#include "GL_tty.h"

/* The RISCV logo, with a tiny resolution
 * (remember, I only got 4Kb of RAM
 * on the IceStick !) 
 */
unsigned char logo[16][16] = {
   {7,7,7,7,7,7,5,3,3,3,3,3,3,3,3,7},
   {7,7,7,7,7,7,7,5,3,3,3,3,3,3,3,7},
   {1,1,1,1,2,7,7,7,3,3,3,3,3,3,3,7},
   {0,0,0,0,0,1,7,7,5,3,3,3,3,3,3,7},
   {0,0,0,0,0,0,7,7,6,3,3,3,3,3,6,7},
   {0,0,0,0,0,0,7,7,5,3,3,3,3,4,7,7},
   {0,0,0,0,0,2,7,7,4,3,3,3,3,7,7,7},
   {0,2,2,2,7,7,7,6,3,3,3,3,6,7,7,7},
   {0,7,7,7,7,7,6,3,3,3,3,5,7,7,2,7},
   {0,1,7,7,7,4,3,3,3,3,3,7,7,7,0,7},
   {0,0,2,7,7,6,3,3,3,3,6,7,7,1,0,7},
   {0,0,0,2,7,7,5,3,3,5,7,7,2,0,0,7},
   {0,0,0,0,7,7,7,5,4,7,7,2,0,0,0,7},
   {0,0,0,0,0,7,7,7,7,7,7,0,0,0,0,7},
   {0,0,0,0,0,1,7,7,7,7,1,0,0,0,0,7},
   {7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}
};

/* 
 * The colormap.
 */
const char* cmap[8] = {
   GL_RGB(040,051,116),
   GL_RGB(123,128,155),
   GL_RGB(170,172,188),
   GL_RGB(249,177,021),
   GL_RGB(249,190,101),
   GL_RGB(249,199,130),
   GL_RGB(252,216,176),
   GL_RGB(250,251,248)
};

/*
 * Generated by make_sintab.c 64 256
 */

int sintab[64] = {
   0,25,49,74,97,120,142,162,181,197,212,225,236,244,251,254,
   256,254,251,244,236,225,212,197,181,162,142,120,97,74,49,25,
   0,-25,-49,-74,-97,-120,-142,-162,-181,-197,-212,-225,-236,-244,
   -251,-254,-256,-254,-251,-244,-236,-225,-212,-197,-181,-162,
   -142,-120,-97,-74,-49,-25
};

void main() {

    int frame = 0;
    for(;;) {
        GL_home();

        int scaling = (sintab[frame&63]+300)*3;
        int Ux = scaling*sintab[frame & 63];         
        int Uy = scaling*sintab[(frame + 16) & 63];  
        int Vx = -Uy;                                
        int Vy =  Ux;                                

        int X0 = -(GL_width/2)*(Ux+Vx);
        int Y0 = -(GL_height/2)*(Uy+Vy);

	for(int y=0; y<GL_height; ++y) {
	    int X = X0;
	    int Y = Y0;
	    for(int x=0; x<GL_width; ++x) {
	        unsigned char col = logo[(Y >> 18)&15][(X >> 18)&15];
                GL_setpixelIhere(cmap, col);
	        X += Ux;
	        Y += Uy;
	    }
            GL_newline();
	    X0 += Vx;
	    Y0 += Vy;
	}
        ++frame;
        GL_swapbuffers();
    }
}
